package com.aote.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.aote.expression.ParamProcessorFactory;
import com.browsesoft.Tools;
import com.browsesoft.user.User;

/**
 * 字符串工具；类
 * 
 * @author Administrator
 * 
 */
public class StringHelper {

	/**
	 * 用map中的属性替换到str中以#属性名#形式出现的同名属性
	 * 
	 * @param map
	 * @param str
	 * @return
	 */
	public static String replaceStr(Map map, String str) {
		String result = replaceStr(map, str, "#");
		return result;
	}

	/**
	 * 参数替换
	 */
	public static String replaceStr(Map map, String str, String c) {
		// 对于字符串里的每一个参数
		StringBuffer sb = new StringBuffer(str);
		int start = sb.indexOf(c);
		while (start != -1) {
			int end = sb.indexOf(c, start + 1);
			if (end == -1) {
				break;
			}
			String name = sb.substring(start + 1, end);
			String value = getValue(map, name);
			// 没有值，不替换
			if (value == null || value.equals("")) {
				start = sb.indexOf(c, end + 1);
			} else {
				sb.replace(start, end + 1, value);
				int size = value.length() - name.length();
				start = sb.indexOf(c, end + size);
			}
		}
		return sb.toString();
	}

	/**
	 * 得到登陆用户进行参数处理
	 */
	private static String getValue(Map map, String name) {
		// 从请求中得到参数值
		String value = (String) map.get(name);
		if (value != null && !value.equals("")) {
			return value;
		}
		User loginUser = (User) ThreadLocals.loginUser.get();
		String result = "";
		try {
			result = ParamProcessorFactory.getInstance().process(name, map,
					loginUser, null, null);
		} catch (Exception e) {
		}
		return result;
	}

	/**
	 * 把字符串的首字符转换为大写
	 */
	public static String upperFirstCharacter(String name) {
		String lower = name.substring(0, 1);
		String upper = lower.toUpperCase();
		name = name.replaceFirst(lower, upper);
		return name;
	}

	/**
	 * 解析属性字符串，型如：attrName1:attrValue1;attrName2:attrValue2;????
	 * 
	 * @param string
	 * @return
	 */
	public static Map parseAttributes(String string) {
		Map result = new HashMap();
		if (string == null || string.equals("")) {
			return result;
		}
		// 解析属性串
		Iterator iter = Tools.stringToLinkedList(string, ';').iterator();
		while (iter.hasNext()) {
			String one = (String) iter.next();
			String[] value = one.split(":");
			result.put(value[0], value[1]);
		}
		return result;
	}

	/**
	 * 把map表示的属性转换成json的格式
	 */
	public static String mapToJSON(Map attrs) {
		StringBuffer result = new StringBuffer();
		result.append('{');
		Iterator iter = attrs.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String) entry.getKey();
			String value = (String) entry.getValue();
			result.append('"').append(key).append('"').append(':').append('"')
					.append(value).append('"').append(',');
		}
		// 替换最后一个逗号为'}'
		int len = result.length();
		result.setCharAt(len - 1, '}');
		return result.toString();
	}

	/**
	 * 字符串是否为空的判断
	 */
	public static boolean isNull(String str) {
		return str == null || str.trim().equals("");
	}

	/**
	 * 给以逗号分割的字符串中每一项添加单引号
	 */
	public static String addQuote(String source) {
		LinkedList list = Tools.stringToLinkedList(source);
		Iterator iter = list.iterator();
		while (iter.hasNext()) {
			String str = (String) iter.next();
			str = "'" + str + "'";
		}
		String result = Tools.LinkedListTostring(list);
		return result;
	}

	/**
	 * 将用某一个符号隔开的一串字符转化为列表
	 */
	public static List getStrings(String string, String tag) {
		List result = new LinkedList();
		if (string == null || string.equals("")) {
			return null;
		}
		StringBuffer buffer = new StringBuffer(string);
		int start = 0, end = 0;
		while (start < buffer.length()) {
			end = buffer.indexOf(tag, start);
			if (end != -1) {
				String str = buffer.substring(start, end);
				result.add(str);
				start = end + 1;
				end = 0;
			} else {
				String str = buffer.substring(start, buffer.length());
				result.add(str);
				start = buffer.length();
			}
		}
		return result;
	}

	/**
	 * 将key、value为字符串得map转换为以":",";"分开得字符串
	 */
	public static String getString(Map map) {
		String result = new String();
		Iterator iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String) entry.getKey();
			String value = (String) entry.getValue();
			if (result == null || result.equals("")) {
				result = key + ":" + value;
			} else {
				result = result + ";" + key + ":" + value;
			}
		}
		return result;
	}
}
